var APP = require("../main/app");
var WIN = require("../api/win");
var AJAX = require("../api/ajax");
module.exports = {
	//广告跳转页面
	toUrl : function(linkUrl) {
		linkUrl = linkUrl.split('|');
		var linkType = linkUrl[0];
		var link = linkUrl[1];
		if(linkType.indexOf('app.') > -1) {
			var method = linkType.replace('app.', 'WIN.');
			if(!link) {
				method += '()';
			} else {
				var params = link.split(',');
				method += '("' + params.join('","') + '")';
			}
			eval(method);
		} else {
			//跳转链接
			if(link) {
				WIN.openUrl(link);
			}
		}
	},
	//检查是否登陆
	checkLogin: function(successCB, errorCB) {
		AJAX.read('Simple-checkLogin', {}, function(ret) {
			var isLogin = ret.data.login;
			if(isLogin == 1) {
				$.isFunction(successCB) && successCB();
			} else {
				$.isFunction(errorCB) && errorCB();
			}
		});
	},
	//初始化jpushRegId
	initJpushRegId:function(regId, callback) {
		AJAX.read('Simple-initJPushRegId', {reg_id : regId}, function() {
			//console.log(regId);
			$.isFunction(callback) && callback();
		});
	},
	//推送检查页面
	checkPage:function(param) {
		var that = this;
		try {
			var params = {};
			if(api.systemType == 'ios') {
				params = param;
			} else {
				params = param.ajpush.extra;
			}
			
			if(params.hasOwnProperty('msg_id') && params.msg_id > 0) {
				that.readMsg(params.msg_id);
			}
			
			that.checkExtra(params);
			
		} catch(e) {}
	},
	//检查参数
	checkExtra:function(params) {
		try {
			if(params.type == 'url') {
				if(params.url.indexOf('http://') > -1) {
					WIN.openUrl(params.url);
				} else if(params.url.indexOf('app.') > -1) {
					var method = params.url.replace('app.', 'WIN.');
					eval(method);
				}
			} else if(params.type == 'event') {
				
			}
		}  catch(e) {}
	},
	//将消息置为已阅读
	readMsg:function(msgId) {
		var that = this;
		AJAX.read('User-messageRead', {id:msgId}, function() {
			$api.sendEvt('messageChange');
		});
	},
	//检查是否需要绑定手机
	checkMobile:function(callback) {
		var that = this;
		AJAX.read('User-checkMobile', {}, function(ret) {
			if(ret.data.status == 1) {
				$.isFunction(callback) && callback();
			} else {
				$api.errorToast('请先绑定手机号码',function() {
					WIN.openBindMobile();
				})
				
			}
		});
	},
}